/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.course.subscribe.util;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.List;

import javax.portlet.PortletRequest;

import org.apache.commons.lang.time.DateFormatUtils;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-11-3下午01:33:33
 */
public class DataTypeUtil {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	/*	Time amTime = DataTypeUtil.parseTimeAMPM("12:21 am");
		System.out.println(amTime);
		Time pmTime = DataTypeUtil.parseTimeAMPM("11:21 pm");
		System.out.println(pmTime);
		System.out.println(renderTimeAMPM(amTime));
		System.out.println(renderTimeAMPM(pmTime));*/
		
		Calendar d = DataTypeUtil.parseCalender("2007-10-23");
		System.out.println(d.getTime());
		System.out.println(DataTypeUtil.renderCalender(d));

	}

	public static Calendar parseCalender(String value){
		if(value==null)return null;
		String[] values=value.split("-");
		if(values==null)return null;
		if(values.length!=3)return null;
		try{
			int year=Integer.parseInt(values[0]);
			int month=Integer.parseInt(values[1]);;
			int day=Integer.parseInt(values[2]);
			Calendar result = new GregorianCalendar();
			result.set(Calendar.MONTH,month-1);
			result.set(Calendar.YEAR, year);
			result.set(Calendar.DAY_OF_MONTH,day);
			return result;
		}catch(Exception e){
			return null;
		}
	}

	public static String renderCalender(Calendar value){
		return DateFormatUtils.ISO_DATE_FORMAT.format(value);
	}
	
	public static String renderSex(boolean value){
		if(value){
			return "女";
		}else{
			return "男";
		}
	}

	public static String renderCalenderCN(Calendar value){
		String cn = DateFormatUtils.ISO_DATE_FORMAT.format(value);
		cn = cn.replaceFirst("-","年");
		cn = cn.replaceFirst("-","月");
		cn = cn+"日";
		return cn;
	}
	
	public static final float ERROR_FLOAT_VALUE=-1;
	public static float parseFloat(String value){
		float periodFloat = DataTypeUtil.ERROR_FLOAT_VALUE;
		try{
			periodFloat = Float.parseFloat(value);
		}catch(Exception e){
			
		}
		return periodFloat;
	}
	public static final boolean ERROR_BOOLEAN_VALUE=false;
	public static boolean parseBoolean(String value){
		boolean periodBoolean = DataTypeUtil.ERROR_BOOLEAN_VALUE;
		try{
			periodBoolean = Boolean.parseBoolean(value);
		}catch(Exception e){
			
		}
		return periodBoolean;
	}	

	public static final int ERROR_INT_VALUE=-1;
	public static int parseInt(String value){
		int periodInt = DataTypeUtil.ERROR_INT_VALUE;
		try{
			periodInt = Integer.parseInt(value);
		}catch(Exception e){
			
		}
		return periodInt;
	}
	
	public static final long ERROR_LONG_VALUE=-1;
	public static long parseLong(String value){
		long periodLong = DataTypeUtil.ERROR_LONG_VALUE;
		try{
			periodLong = Long.parseLong(value);
		}catch(Exception e){
			
		}
		return periodLong;
	}
	
	public static String _AM=" am";
	public static String _PM=" pm";	
	//10:30 pm
	public static Time parseTimeAMPM(String value){
		if(value==null)return null;
		if(value.endsWith(_AM)){
			return Time.valueOf(value.replace(_AM, ":00"));
		}else{
			if(value.endsWith(_PM)){
				String tValue = value.replace(_PM, ":00");
				String[] values=tValue.split(":");
				if(values==null)return null;
				if(values.length!=3)return null;
				int h=Integer.parseInt(values[0]);
				String pmValue= (h+12)+":"+values[1]+":"+values[2];
				return Time.valueOf(pmValue);
			}else{
				return null;
			}
		}
	}
	
	public static String renderTimeAMPM(Time vTime){
		String tValue = DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(vTime);
		String[] values=tValue.split(":");
		if(values==null)return null;
		if(values.length!=3)return null;
		int h=Integer.parseInt(values[0]);	
		if(h>12){
			return (h-12)+tValue.substring(2,tValue.length()-3)+_PM;
		}else{
			return tValue.substring(0,tValue.length()-3)+_AM;			
		}
	}
	
	public static List<Long> getParamLongList(String startParam,PortletRequest portletRequest){
		Enumeration paramEnum = portletRequest.getParameterNames();
		List<Long> paramIdList = new ArrayList<Long>();
		while(paramEnum.hasMoreElements()){
			Object keyObj = paramEnum.nextElement();
			if((keyObj!=null)&&(keyObj instanceof String)){
				String key = (String)keyObj;
				if(key.startsWith(startParam)){
					String value = portletRequest.getParameter(key);
					long valueLong = DataTypeUtil.parseLong(value);
					paramIdList.add(valueLong);
				}
			}	
		}
		return paramIdList;
	}

}
